<template>
  <div class="sametime_container">
    <div
      class="null_data_one"
      v-if="lastYear.length === 0 && thisYear.length === 0"
    >
      暂无数据
    </div>
    <div id="echarts" style="height: 400px; width: 800px"></div>
  </div>
</template>

<script>
import { theSamePeriod } from '@/request/store/K_PerformanceAnalysis'
import { deepClone } from '@/util/util'
export default {
  data() {
    return {
      lastYear: [],
      thisYear: [],
      option: {
        xAxis: {
          type: 'category',
          data: []
        },
        legend: {
          data: ['今年', '去年']
        },
        yAxis: {
          name: '销售金额',
          type: 'value'
        },
        series: [
          {
            name: '今年',
            data: [],
            type: 'line',
            color: '#017aff'
          },
          {
            name: '去年',
            data: [],
            type: 'line',
            color: '#22ac38'
          }
        ]
      }
    }
  },

  mounted() {
    this.creatEcharts()
    this.theSamePeriod()
  },
  methods: {
    async theSamePeriod() {
      const { data } = await theSamePeriod()
      console.log(data, '同期')
      if (data && data.code === 200) {
        this.lastYear = data.data.lastYear
        this.thisYear = data.data.thisYear
        // console.log(this.thisYear)
        this.setOPtion()
      }
    },
    creatEcharts() {
      let echarts = this.$echarts.init(document.getElementById('echarts'))
      echarts.setOption(this.option)
    },
    setOPtion() {
      let option = deepClone(this.option)
      let thisYear = deepClone(this.thisYear)
      // 今年
      thisYear.forEach((item, index) => {
        if (item.sellTimeByString) {
          let sellTimeByString = item.sellTimeByString.split('-')[1]
          let month = `${sellTimeByString}月`
          option.series[0].data.push(item.money)
          option.xAxis.data.push(month)
        }
      })
      // 去年
      // lastYear.forEach((item, index) => {
      //   if (item.sellTimeByString) {
      //     let sellTimeByString = item.sellTimeByString.split('-')[1]
      //     let month = `${sellTimeByString}月`
      //     option.series[1].data.push(item.money)
      //     option.xAxis.data.push(month)
      //   }
      // })
      this.option = option
      this.creatEcharts()
    }
  }
}
</script>

<style lang="less" scoped>
.sametime_container {
  position: relative;
  .null_data_one {
    position: absolute;
    top: 180px;
    left: 350px;
  }
}

.sameTime {
  background-color: #fff;
}
</style>
